package 函数式接口;

import org.junit.Test;

import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * Description
 *
 * @Author: SunAo
 * @Date: 2021/2/25 15:22
 */
public class 函数式接口学习_Function {

    /**
     * 传入一个值，返回一个值
     */
    @Test
    public void Function简单试用(){
        Function<Integer,String> func = i -> "Number:"+i;
        String r = func.apply(1);
        System.out.println(r);
    }

    @Test
    public void BiFunction简单试用(){
        BiFunction<Integer,String,String> func = (i, s)->s+":"+i;
        String r = func.apply(1,"Number");
        System.out.println(r);
    }

    @Test
    public void Function_compose方法试用1(){
        Function<Integer,String> func = i -> "Number:"+i;
        Function<String,Integer> c = str->Integer.parseInt(str);
        String r =  func.compose(c).apply("1");
        System.out.println(r);
    }

    @Test
    public void Function_compose方法试用2(){
        Function<Integer,String> func = i -> "Number:"+i;
        String r =  func.compose((Long l)->l.intValue()).apply(1L);
        System.out.println(r);
    }
    @Test
    public void Function_compose方法试用3(){
        Function<Integer,String> func = i -> "Number:"+i;
        String r =  func
                .compose((Long l)->l.intValue())
                .compose((String s)->Long.parseLong(s))
                .apply("1");
        System.out.println(r);
    }


    @Test
    public void Function_andThen方法试用(){
        Function<Integer,String> func = i -> "Number:"+i;
        String result = func.andThen(s->"Final Result:"+s).apply(1);
        System.out.println(result);
    }




    @Test
    public void Function_identity方法试用(){
        Function<String,String> func = t->t;
        String result = func.apply("haha");
        System.out.println(result);

        Function<String,String> func2 = Function.identity();
        String result2 = func2.apply("asdf");
        System.out.println(result2);

        Function<Function<String,String>,String> func3 = f -> f.apply("haha1");
        String result3 = func3.apply(Function.identity());
        System.out.println(result3);
    }

}
